package com.example.a2205_fx;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.a2205_fx.base.BaseActivity;
import com.example.a2205_fx.databinding.ActivityMainBinding;
import com.example.a2205_fx.entity.UserEntity;
import com.example.a2205_fx.login.LoginViewModel;
import com.example.a2205_fx.register.RegisterActivity;

public class MainActivity extends BaseActivity<ActivityMainBinding, LoginViewModel> {




    @Override
    protected void initView() {
        if(getIntent().getExtras()!=null){
            String username = (String) getIntent().getExtras().get("username");
            String password = (String) getIntent().getExtras().get("password");
            binding.username.setText(username);
            binding.password.setText(password);
        }
        binding.gsy.setUp("http://txmov6.a.yximgs.com/upic/2020/08/29/22/BMjAyMDA4MjkyMjE3MDJfNDY2MzI3NDE4XzM1MjQxOTA4MTEyXzFfMw==_b_Bec45da0ae9885dd11b4202fa79ee15d2.mp4?tag=1-1599265808-vdg-1-ss8tmjx6ad-a0edd9682705fd1d&tt=b&bp=10000",true,"");
        binding.gsy.startPlayLogic();
        binding.gsy.getBackButton().setVisibility(View.GONE);

        binding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.username.getText().toString();
                String password = binding.password.getText().toString();
                if(username.isEmpty()||password.isEmpty()){
                    ToastUtils.showLong("用户名和密码均不能为空");
                    return;
                }
                if(!RegexUtils.isMobileSimple(username)){
                    ToastUtils.showLong("用户名为手机号");
                    return;
                }
                if(!RegexUtils.isMatch("^[\\w]{6,20}$",password)){
                    ToastUtils.showLong("密码为8-20位的数字和字母");
                    return;
                }
                viewModel.login(username,password);
            }
        });

    }
    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<UserEntity>() {
            @Override
            public void onChanged(UserEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                if(entity.getCode()==200){
                    SPUtils.getInstance().put("token",entity.getData().getToken());
                    SPUtils.getInstance().put("username",entity.getData().getUsername());
                    SPUtils.getInstance().put("icon",entity.getData().getIcon());
                    startActivity(new Intent(MainActivity.this,DouActivity.class));
                }else{
                    startActivity(new Intent(MainActivity.this, RegisterActivity.class));
                }
            }
        });

    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }
}